package com.yuxi;

/**
 * Created by yuxi on 17/1/28.
 */
public class SynchronizedOne {
    //当一个线程访问某个对象的同步代码块或者同步方法时其他对象对这个同步方法或者同步代码块的访问是阻塞的
    public static void main(String[] args) {
        Object o = new Object();
        Sn sn = new Sn(o);
        //分别启动三个线程
        new Thread(sn).start();
        new Thread(sn).start();
        new Thread(sn).start();
    }

    static class Sn implements Runnable {
        private Object object;

        public Sn(Object object) {
            this.object = object;
        }

        public void run() {
            test1(object);
        }
    }

    public static void test1(Object object) {
        synchronized (object) {
            try {
                System.out.println(Thread.currentThread().getName() + "....." + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName() + "....." + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
